package com.xgkj.diyiketang.util;

import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;

/**
 * 项目名称：Sosoliuda
 * 类描述：获取资源
 * 创建人：周作威
 * 创建时间：2016/11/30
 */

public class ResourcesUtils {
    /**
     * 获取字符串
     *
     * @param id
     * @param obj
     * @return
     */
    public static String getString(@StringRes int id, Object... obj) {
        String string = EatShowApp.getInstance().getResources().getString(id);
        if (null != obj && obj.length > 0)
            return String.format(string, obj);
        return string;
    }

    /**
     * 获取颜色
     *
     * @param color
     * @return
     */
    public static int getColor(@ColorRes int color) {
        return EatShowApp.getInstance().getResources().getColor(color);
    }

    /**
     * 获取图片
     *
     * @param drawable
     * @return
     */
    public static Drawable getDrawable(@DrawableRes int drawable) {
        return EatShowApp.getInstance().getResources().getDrawable(drawable);
    }

    /**
     * 文字中添加图片
     *
     * @param textView
     * @param string
     * @param imgResId
     * @param index
     * @param padding
     */
    public static void setTvaddDrawable(TextView textView, String string, @DrawableRes int imgResId, int index, int padding) {
        textView.setText(string);
        if (imgResId == -1) {
            textView.setCompoundDrawables(null, null, null, null);
        } else {
            Drawable drawable = getDrawable(imgResId);
            // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            textView.setCompoundDrawables(index == 1 ? drawable : null, index == 2 ? drawable : null, index == 3 ? drawable : null, index == 4 ? drawable : null);
            textView.setCompoundDrawablePadding(padding);
        }
    }
}
